package mpcs.view
{
	import flash.events.Event;
	
	import mpcs.NotiConst;
	import mpcs.model.vo.UserVO;
	import mpcs.view.components.RegForm;
	
	import mx.controls.Alert;
	
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.mediator.Mediator;
	
	/**
	 *  
	 * @author zhangzuoqiang
	 */
	public class RegMediator extends Mediator {
		
		public static var NAME:String = "RegMediator";
		
		public function RegMediator( viewComponent:RegForm) {
			super(NAME, viewComponent);
		}
		
		override public function onRegister():void {
			regForm.addEventListener( RegForm.REGISTER, onRegistor);
		}
		
		public function removeListeners():void {
			regForm.removeEventListener( RegForm.REGISTER, onRegistor);
		}
		
		//用户注册请求
		private function onRegistor( event:Event ):void {
			if ( regForm.regEmail.text == ""){
				regForm.emailInputColor = "0xFF0000";
				regForm.regEmail.setFocus();
				return;
			}else if(regForm.regRepwd.text == ""){
				regForm.pwdInputColor  = "0xFF0000";
				regForm.regRepwd.setFocus();
				return;
			}else{
				var uservo:UserVO = new UserVO(regForm.regEmail.text, regForm.regRepwd.text);
				// 发送注册请求
				sendNotification( NotiConst.S_REGISTER, uservo);
				trace("sendNotification( NotiConst.S_REGISTER, uservo) ... ");
				regForm.btn_register.enabled = false;
			}
		}
		
		override public function listNotificationInterests() : Array {
			return [
				NotiConst.R_REGISTER_SUCCESS,
				NotiConst.R_REGISTER_FAILED
			];
		}
		
		override public function handleNotification(notification:INotification) : void {
			var name:String = notification.getName();
			switch(name){
				case NotiConst.R_REGISTER_SUCCESS:
					regForm.regFormHeading.label = "用户注册成功, 请返回登录面板。"
					trace("case NotiConst.R_REGISTER_SUCCESS ... ");
					break;
				case NotiConst.R_REGISTER_FAILED:
					Alert.show( "错误代码: " + notification.getBody() , "注册失败");
					trace("case NotiConst.R_REGISTER_FAILED ... " + notification.getBody() );
					break;
			}
		}
		
		private function get regForm():RegForm {
			return viewComponent as RegForm;
		}
		
	}
}